package com.hsource.doctorcenter.service.healthRecords.impl;

import cn.hutool.core.convert.Convert;
import com.hsource.common.utils.DateUtil;
import com.hsource.doctorcenter.conf.dataSource.aspect.SnowflakeIdWorker;
import com.hsource.doctorcenter.domain.healthRecords.HealthRecords;
import com.hsource.doctorcenter.mapper.HealthRecordsMapper;
import com.hsource.doctorcenter.service.healthRecords.IHealthRecordsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.List;

/**
 * 健康档案Service业务层处理
 *
 * @author yangyang
 * @date 2020-09-25
 */
@Service
public class HealthRecordsServiceImpl implements IHealthRecordsService {
    @Autowired
    private HealthRecordsMapper healthRecordsMapper;

    /**
     * 查询健康档案
     *
     * @param id 健康档案ID
     * @return 健康档案
     */
    @Override
    public HealthRecords selectHealthRecordsById(String id) {
        return healthRecordsMapper.selectHealthRecordsById(id);
    }

    /**
     * 查询健康档案列表
     *
     * @param healthRecords 健康档案
     * @return 健康档案
     */
    @Override
    public List<HealthRecords> selectHealthRecordsList(HealthRecords healthRecords) {
        return healthRecordsMapper.selectHealthRecordsList(healthRecords);
    }

    /**
     * 查询健康档案列表
     *
     * @param healthRecords 健康档案
     * @return 健康档案集合
     */
    @Override
    public List<HealthRecords>  selectHealthRecordsListByUserId(HealthRecords healthRecords){
        return healthRecordsMapper.selectHealthRecordsListByUserId(healthRecords);
    }


    /**
     * 新增健康档案
     *
     * @param healthRecords 健康档案
     * @return 结果
     */
    @Override
    public int insertHealthRecords(HealthRecords healthRecords) throws Exception {
        SnowflakeIdWorker idWorker = new SnowflakeIdWorker(1, 1);
        long l = idWorker.nextId();
        healthRecords.setId(String.valueOf(l));
        healthRecords.setCreateTime(Calendar.getInstance().getTime());
        healthRecords.setBirthday(DateUtil.getDateFromStr(healthRecords.getBirthdayDate()+" 00:00:00",""));
        healthRecords.setAge(DateUtil.getAge(healthRecords.getBirthday()));
        return healthRecordsMapper.insertHealthRecords(healthRecords);
    }

    /**
     * 修改健康档案
     *
     * @param healthRecords 健康档案
     * @return 结果
     */
    @Override
    public int updateHealthRecords(HealthRecords healthRecords) throws Exception {
        healthRecords.setAge(DateUtil.getAge(healthRecords.getBirthday()));
        return healthRecordsMapper.updateHealthRecords(healthRecords);
    }

    /**
     * 删除健康档案对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteHealthRecordsByIds(String ids) {
        return healthRecordsMapper.deleteHealthRecordsByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除健康档案信息
     *
     * @param id 健康档案ID
     * @return 结果
     */
    @Override
    public int deleteHealthRecordsById(String id) {
        return healthRecordsMapper.deleteHealthRecordsById(id);
    }
}
